#include "Fleet.h"
#include "Corvette.h"
#include "AircraftCarrier.h"
#include "Cruiser.h"
#include "Destroyer.h"
#include "Submarine.h"

Fleet :: Fleet(const string& _name) : Boat(_name, Point<int>(0, 0)) { }

Fleet :: ~Fleet() {
	list<Boat*>::iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		delete *i;
	}
}

bool Fleet :: fireToBoat(const Point<int>& pt, Arena* ar) {
	list<Boat*>::iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if ((*i)->fireToBoat(pt, ar)) return true;
	}
	return false;
}

void Fleet :: write(Arena* ar) throw (OutOfBound, OverlapBoat, TooNearBoat) {
	list<Boat*>::iterator i = ships.begin();
		for (i = ships.begin(); i != ships.end(); ++i) {
			(*i)->write(ar);
		}
}

bool Fleet :: isDestroyed() {
	list<Boat*>::iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (!(*i)->isDestroyed()) return false;
	}
	return true;
}

void Fleet :: add(Boat* bt) {
	ships.push_back(bt);
}

int Fleet :: numberActiveBoat() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (!(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberActiveCorvette() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Corvette*>(*i) && !(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberActiveCruiser() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Cruiser*>(*i) && !(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberActiveAircraftCarrier() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<AircraftCarrier*>(*i) && !(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberActiveSubmarine() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Submarine*>(*i) && !(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberActiveDestroyer() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Destroyer*>(*i) && !(*i)->isDestroyed()) ++number;
	}
	return number;
}

int Fleet :: numberBoat() const {
	return ships.size();
}

int Fleet :: numberCorvette() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Corvette*>(*i)) ++number;
	}
	return number;
}

int Fleet :: numberCruiser() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Cruiser*>(*i)) ++number;
	}
	return number;
}

int Fleet :: numberAircraftCarrier() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<AircraftCarrier*>(*i)) ++number;
	}
	return number;
}

int Fleet :: numberSubmarine() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Submarine*>(*i)) ++number;
	}
	return number;
}

int Fleet :: numberDestroyer() const {
	int number = 0;
	list<Boat*>::const_iterator i = ships.begin();
	for (i = ships.begin(); i != ships.end(); ++i) {
		if (dynamic_cast<Destroyer*>(*i)) ++number;
	}
	return number;
}

